package com.graduate.daydayup.activity

import android.os.Bundle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.graduate.daydayup.App
import com.graduate.daydayup.R
import com.graduate.daydayup.base.BaseActivity
import com.graduate.daydayup.room.Account
import com.graduate.daydayup.room.MyRoomDataBase
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.activity_register.et_account
import kotlinx.android.synthetic.main.activity_register.et_pass

class LoginActivity :BaseActivity() {
    override fun setTitle()=""

    override fun getLayout(): Int{
        if (SPUtils.getInstance().getString("account").isNotEmpty()&& SPUtils.getInstance().getString("password").isNotEmpty()){
            ActivityUtils.startActivity(MainActivity::class.java)
            finish()
        }
            return  R.layout.activity_login
    }

    override fun setTextRight()=""

    override fun setImgRightListener() {

    }

    override fun isAddToolbar()=false

    override fun initViewIds() {

    }

    override fun initView() {
        if ( SPUtils.getInstance().getString("account")!=null&&!SPUtils.getInstance().getString("account").isEmpty()){
            et_account.setText(SPUtils.getInstance().getString("account"))
            et_pass.setText(SPUtils.getInstance().getString("password"))
        }
        btn_login.setOnClickListener {
            if (et_account.text.toString().isEmpty()){
                ToastUtils.showShort("账户为空")
            }else if (et_pass.text.toString().isEmpty()){
                ToastUtils.showShort("密码为空")
            }else{
                val allByAccount = MyRoomDataBase.getInstance(this).accountDao().getAllByAccountPass(et_account.text.toString(), et_pass.text.toString())
                if (allByAccount!=null&& allByAccount.account.isNotEmpty()){
                    ToastUtils.showShort("登录成功")
                    SPUtils.getInstance().put("account",et_account.text.toString())
                    SPUtils.getInstance().put("password",et_pass.text.toString())
                    ActivityUtils.startActivity(MainActivity::class.java)
                    finish()
                }else{
                    ToastUtils.showShort("账号密码错误")
                }

            }
        }

        btn_reg.setOnClickListener {
            ActivityUtils.startActivity(RegisterActivity::class.java)
        }
    }

    override fun initData(savedInstanceState: Bundle?) {

    }
}